﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CallCenter.Model;

namespace CallCenter.DAL
{
    class ScriptDAL
    {
        //public ScriptsModel ToT_Scripts(DataRow row)
        //{
        //    ScriptsModel T_Scripts = new ScriptsModel();
        //    T_Scripts.Id = (int)row["Id"];
        //    T_Scripts.ParentId = (int?)row["ParentId"];
        //    T_Scripts.Name = (string)row["Name"];
        //    //T_Scripts.Body = (string)row["Body"];
        //    return T_Scripts;
        //}

        public ScriptsModel ToScript(DataRow row)
        {
            ScriptsModel Script = new ScriptsModel();
            Script.Id = row.IsNull("Id") ? null : (int?)row["Id"];
            Script.ParentId = row.IsNull("ParentId") ? null : (int?)row["ParentId"];
            Script.Name = row.IsNull("Name") ? null : (string)row["Name"];
            Script.Body = row.IsNull("Body") ? null : (string)row["Body"];
            return Script;
        }

        public ScriptsModel[] ListAll()
        {
            DataTable table = SqlHelper.ExecuteDataTable("select * from T_Scripts");
            if (table.Rows.Count <= 0)
            {
                return null;
            }
            else
            {
                ScriptsModel[] T_Scripts = new ScriptsModel[table.Rows.Count];
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ScriptsModel model = ToScript(table.Rows[i]);
                    T_Scripts[i] = model;
                }
                return T_Scripts;
            }
        }

        public ScriptsModel[] GetByParentId(int ParentId)
        {
            DataTable table = SqlHelper.ExecuteDataTable("select * from T_Scripts where ParentId = @ParentId",
                new SqlParameter("@ParentId", ParentId));
            ScriptsModel[] scripts = new ScriptsModel[table.Rows.Count];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                ScriptsModel model = new ScriptsModel();
                DataRow row = table.Rows[i];
                model = ToScript(row);
                scripts[i] = model;
            }
            return scripts;
        }

        public int Update(ScriptsModel model)
        {
            return SqlHelper.ExecuteNonQuery(@"update T_Scripts set 
                Name=@Name,Body=@Body where Id=@Id",
            new SqlParameter("@Id", model.Id),
            new SqlParameter("@Name", model.Name),
            new SqlParameter("@Body", model.Body));
        }

        public int AddNew(ScriptsModel model)
        {
            return SqlHelper.ExecuteNonQuery(@"insert into T_Scripts(
                                    ParentId,Name,Body) output inserted.Id
                                    values (
                                    @ParentId,@Name,@Body)",
            new SqlParameter("@ParentId", model.ParentId),
            new SqlParameter("@Name", model.Name),
            new SqlParameter("@Body", model.Body));
        }

        public void DeleteAllNode(int id)
        {
            ScriptsModel[] scripts =GetByParentId(id);
            foreach (ScriptsModel script in scripts)
            {
                DeleteAllNode((int)script.Id);
            }
            SqlHelper.ExecuteNonQuery("delete from T_Scripts where Id=@id",
                new SqlParameter("@id", id));
        }
    }
}
